package priv.pront.code.national.acwing.dp.linear;

import java.util.Scanner;

public class A897_最长公共子序列 {
    static int n, m;
    static int[][] dp;
    static char[] a, b;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        String s1 = scanner.next();
        String s2 = scanner.next();
        a = new char[n + 1];
        b = new char[m + 1];
        for (int i = 1; i <= n; i++) {
            a[i] = s1.charAt(i - 1);
        }
        for (int i = 1; i <= m; i++) {
            b[i] = s2.charAt(i - 1);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                if (a[i] == b[j]) dp[i][j] = Math.max(dp[i - 1][j - 1] + 1, dp[i][j]);
            }
        }
        System.out.println(dp[n][m]);
    }
}
